package btp.Calendars;

public class IslamicCalendar {

	//It is a straightforward and strictly lunar calendar with no intercalation. 
	//The average year is about 354+11/30 days long and therefore the months move wrt to the seasons, in a period of about 32 years.
	//Days begin at sunset.
	//This is an arithmetic calendar. The observational calendar is used for most religious purposes.
	//The arithmetica calendar is used as an estimate.
	
	//Days
	public static final int YAUM_AL_AHAD = 0; //FIRST DAY
	public static final int YAUM_AL_ITHNAYNA = 1; //SECOND DAY
	public static final int YAUM_ATH_THALATHA = 2; //THIRD DAY
	public static final int YAUM_AL_ARBA_A = 3; //FOURTH DAY
	public static final int YAUM_AL_HAMIS = 4; //FIFTH DAY
	public static final int YAUM_AL_JUM_A = 5; //DAY FOR ASSEMBY
	public static final int YAUM_AL_SABT = 6; //DAY OF THE SABBATH
	
	//The epoch is Thursday, July 15, 662(Julian), the year od Mohammed's emigration to Madina.
	//We denote July 16, 662 (Julian) as the beginning of the Islamic year 1 (Muharram 1, A.H. 1). AH=Anno Hagira.
	public static final long EPOCH = 227015; // JulianCalendar.getRDFromJulian(662, JulianCalendar.JULY, 16);
	
	//The 12 Islamic months which have an alternating strucutre of 29 and 30 days starting with 30 (Muharram).
	public static final int MUHARRAM = 1;
	public static final int SAFAR = 2;
	public static final int RABI_AL_AWWAL = 3;
	public static final int RABI_AL_AHIR = 4;
	public static final int JUMADA_AL_ULA = 5;
	public static final int JUMADA_AL_AHIRA = 6;
	public static final int RAJAB = 7;
	public static final int SHABAN = 8;
	public static final int RAMADAN = 9;
	public static final int SHAWWAL = 10;
	public static final int DHU_AL_QA_DA = 11;
	public static final int DHU_AL_HIJJA = 12; //this has 30 days in a leap year. The leap years are 2, 5, 7, 10, 13, 16, 18, 21, 26, 29 in a 30-year cycle.
		
	int year;
	int month;
	int day;
	
	
	public IslamicCalendar(int year, int month, int day) {
		this.year = year;
		this.month = month;
		this.day = day;
	}
	
	//There are variants to this leap year structure. Some take year 15 to be leap year instead of 16, including some of the Microsoft products!!!
	//The Bohras (A sect of about 1 million in India) take the Epoch a day earlier and use (2, 5, 8, 10, 13, 16, 19, 21, 24, 27, 29) as the leap years.
	public boolean isLeapYear(){
		return ((14+11*year)%30+30)%30 < 11;
	}
	
	public long getRDFromCalendar(){
		return (long) (EPOCH-1+(year-1)*354+Math.floor((3+11*year)/30.0)+29*(month-1)+Math.floor(month/2.0)+day);	
	}
	
	public static IslamicCalendar getCalendarFromRD(long rd){
		int year = (int) Math.floor((30*(rd-EPOCH)+10646)/10631.0);
		IslamicCalendar ic = new IslamicCalendar(year, 1, 1);
		int priorDays = (int) (rd - ic.getRDFromCalendar());
		int month = (int) Math.floor((11*priorDays+330)/325.0);
		ic.setMonth(month);
		int day = (int) (1+rd-ic.getRDFromCalendar());
		ic.setDay(day);
		return ic;
	}

	public final int getYear() {
		return year;
	}

	public final void setYear(int year) {
		this.year = year;
	}

	public final int getMonth() {
		return month;
	}

	public final void setMonth(int month) {
		this.month = month;
	}

	public final int getDay() {
		return day;
	}

	public final void setDay(int day) {
		this.day = day;
	}
	
	
}
